package com.hzw.saas.web.admin.hpc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.hpc.IHpcAccountProviderService;
import com.hzw.saas.api.hpc.bo.HpcAccountProviderBo;
import com.hzw.saas.api.hpc.enums.HpcProviderType;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.ValidateList;
import com.hzw.saas.web.admin.hpc.param.HpcProviderParam;
import io.swagger.annotations.*;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/3/25 17:34
 */
@Api(tags = "HPC/供应商")
@RestController
@RequiredArgsConstructor
@RequestMapping("/hpc/provider")
public class HpcProviderController {

    @Resource(name = "hpcAccountProviderServiceImpl")
    private IHpcAccountProviderService hpcAccountProviderService;

    private final MapperFacade mapperFacade;


    @PostMapping("/")
    @ApiOperation(value = "添加HPC供应商账号", notes = "添加HPC供应商账号，支持批量")
    @ApiOperationSort(1)
    @ApiResponses({
        @ApiResponse(code = 200, message = "导入成功")
    })
    public ResponseEntity<Void> addBatchHcpAccount(@Validated @RequestBody ValidateList<HpcProviderParam> hpcProviderList) {
        hpcAccountProviderService.addBatch(mapperFacade.mapAsList(hpcProviderList, HpcAccountProviderBo.class));
        return ResponseEntity.ok().build();
    }


    @GetMapping("/")
    @ApiOperation(value = "查询HPC供应商账号", notes = "查询HPC供应商账号，每个供应商账号可关联多个HPC账号")
    @ApiOperationSort(2)
    public ResponseEntity<IPage<HpcAccountProviderBo>> addBatchHcpAccount(PageParam pageParam,
                                                                          @RequestParam(required = false, defaultValue = "DQ01") @ApiParam("供应商类型") String hpcType,
                                                                          @RequestParam(required = false) @ApiParam("模糊检索，一般为供应商名称") String fuzzy) {
        return ResponseEntity.ok(hpcAccountProviderService.listByPage(pageParam, HpcProviderType.instance(hpcType), fuzzy));
    }

    @PutMapping("/")
    @ApiOperation(value = "更新HPC供应商账户")
    public ResponseEntity<Void> updateProvider(@Validated @RequestBody HpcProviderParam hpcProviderParam) {
        String providerId = hpcProviderParam.getProviderId();
        AssertUtil.assertThrow("操作失败,更新的HPC供应商账户ID为空", StringUtil.isBlank(providerId));
        HpcAccountProviderBo hpcAccountProviderBo = mapperFacade.map(hpcProviderParam, HpcAccountProviderBo.class);
        hpcAccountProviderService.update(hpcAccountProviderBo);
        return ResponseEntity.ok().build();
    }

    @ApiOperationSort(3)
    @DeleteMapping("/{providerId}")
    @ApiOperation(value = "删除HPC供应商账户")
    public ResponseEntity<Void> deleteProvider(@PathVariable String providerId) {
        hpcAccountProviderService.deleteProvider(providerId);
        return ResponseEntity.ok().build();
    }

}
